<template>
  <header>
    <div class="header-upper">
      <div class="header-wrap">
        <div class="header-user">
          <span>小米商城欢迎您！</span>

          <span class="user-span" v-if="!userName"
            >请
            <router-link to="/login">登录</router-link>
            |
            <router-link to="/signin">注册</router-link>
          </span>
          <span v-else class="user-span">
            {{ userName }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button @click="logout">退出登入</button>
          </span>
        </div>
        <ul class="header-nav">
          <li>
            <router-link to="/order">我的订单</router-link>
          </li>
          <li>我的收藏</li>
          <li class="nav-li iconfont icon-shopping_cart" icon="ShoppingTrolley">
            <router-link to="/shoppingcart">购物车 (0)</router-link>
          </li>
        </ul>
      </div>
    </div>
    <div class="header-below">
      <img src="./images/logo.c8999c02.png" alt="logo" class="header-logo" />
      <ul class="below-nav">
        <li
          v-for="(item, index) in items"
          :key="index"
          :class="activeIndex === index ? 'active' : ''"
          @click="addClass(index)"
        >
          <router-link :to="index === 0 ? '/' : '/goods'">
            {{ item.title }}</router-link
          >
        </li>
      </ul>
      <form class="header-form" @submit.prevent="fn">
        <input
          type="text"
          class="header-input"
          placeholder="请输入搜索内容"
          v-model="search"
        />
        <button class="header-btn iconfont icon-search">搜索</button>
      </form>
    </div>
  </header>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'wHeader',
  data() {
    return {
      activeIndex: 0, // 0为默认选中第一项，-1为不选
      items: [{ title: '首页' }, { title: '全部商品' }, { title: '关于我们' }],
      search: ''
    }
  },
  computed: {
    ...mapState('user', {
      userName: (state) => state.userName
    })
  },
  methods: {
    ...mapMutations('user', ['LOG_Out']),
    addClass(index) {
      this.activeIndex = index // 把当前所点击元素的index，赋值给activeIndex
    },
    fn() {
      const search = this.search
      const query = {
        ...this.$route.query
      }
      // input为空就删掉keyword的值
      delete query.search
      if (search) {
        query.search = search
        // query.categoryID = undefined
        delete query.categoryID // input为空就删掉keyword的值
      }

      this.$router.push({
        name: 'Goods',
        query: query
      })
    },
    logout() {
      this.LOG_Out()
      this.$message({
        type: 'success',
        message: '退出登入成功'
      })
    }
  },
  watch: {
    search: {
      handler(val) {
        if (!val) {
          console.log(val)
          delete this.$route.query.search
        }
      }
    }
  }
}
</script>

<style scoped>
.header-upper {
  font-size: 14px;
  height: 40px;
  background-color: #3d3d3d;
  color: #b0b0b0;
}
.header-wrap {
  width: 1200px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 auto;
  height: 100%;
}
.user-span:hover {
  cursor: pointer;
}
.header-nav {
  display: flex;
}
.header-nav li {
  margin-left: 20px;
  line-height: 40px;
  cursor: pointer;
}
.header-nav li:hover {
  color: #e6e6e6;
}
.nav-li {
  width: 120px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.nav-li:hover {
  color: #ff6700 !important;
  background-color: #fff;
}
.header-below {
  font-size: 14px;
  width: 1200px;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px auto;
  border-bottom: 1px solid #e6e6e6;
}
.below-nav {
  display: flex;
  padding-right: 290px;
  cursor: pointer;
}
.below-nav li {
  height: 59px;
  padding: 0 20px;
  line-height: 59px;
  color: #909399;
}
.below-nav li:hover {
  color: #3d3d3d;
}
.header-input {
  height: 30px;
  width: 230px;
  padding: 0 20px;
  border: 1px solid #e6e6e6;
  outline: none;
}
.header-btn {
  width: 60px;
  height: 32px;
  border: none;
  background-color: #f5f7fa;
  cursor: pointer;
  font-size: 12px;
  color: #001;
  vertical-align: top;
}
.active {
  border-bottom: 2px solid #409eff;
}
.router-link-avtive {
  text-decoration: none;
  color: #fff;
}
a {
  text-decoration: none;
  color: #b0b0b0;
}
a:hover {
  color: #e6e6e6;
}
.nav-li a:hover {
  color: #ff6700 !important;
  background-color: #fff;
}
</style>
